﻿using System;
using System.ComponentModel;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using BusinessServices;
using Core.Common;
using DataEntities;

namespace CarLeaseManageSystem
{
    public partial class FormMain : Form
    {
        private readonly SettingService _settingService;
        private readonly ChuZuService _chuZuService;
        private readonly YuZuService _yuZuService;
        private readonly Setting _setting;
        private string sCaption = "";

        public FormMain()
        {
            _settingService = new SettingService();
            _chuZuService = new ChuZuService();
            _yuZuService = new YuZuService();
            _setting = _settingService.Get();
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            if (_setting.bShowCaption)
            {
                labZiMu.Visible = true;
                sCaption = _setting.Caption;
            }

            if (_setting.bShowDate)
            {
                labShowTime.Visible = true;
            }

            if (_setting.bShowMsg)
            {
                LabMsg.Visible = true;
            }

            string prodCode = _setting.ProdCode;
            if (string.IsNullOrEmpty(prodCode) || !string.Equals(prodCode, AppSettings.ProductCode, StringComparison.OrdinalIgnoreCase))
            {
                var days = (DateTime.Now - _setting.dtRegidate).Days;
                if (days >= 6)
                {
                    this.Text += "(6天试用期已到期,请注册.)";
                    toolsMain.Enabled = false;
                }
                else
                {
                    this.Text += "(6天试用版)";
                }
            }
            else
            {
                this.Text += "(已注册)";
            }

            GetMsg();
        }

        private void GetMsg()
        {
            var stringBuilder = new StringBuilder();
            int n1 = 0, n2 = 0, n3 = 0;

            if (_setting.bShowCQ)
            {
                n1 = _chuZuService.CountTimeOverChuZuInfo();
                stringBuilder.AppendFormat("今日您有[{0}]条超期记录待处理.\n", n1);
            }
            if (_setting.bShowDQ)
            {
                n2 = _chuZuService.CountTimeOutChuZuInfo();
                stringBuilder.AppendFormat("今日您有[{0}]条到期记录待处理.\n", n2);
            }
            if (_setting.bShowYD)
            {
                n3 = _yuZuService.GetPreconcertCount();
                stringBuilder.AppendFormat("今日您有[{0}]条预定记录待处理.", n3);
            }

            var str = stringBuilder.ToString();
            if (!string.IsNullOrEmpty(str))
            {
                LabMsg.Text = string.Format("今日待处理:超期[{0}]条 到期[{1}]条 预定[{2}]条", n1, n2, n3);
                MessageBox.Show(str, "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Logout(object sender, EventArgs e)
        {
            bShow = false;
            timerShowWin.Enabled = true;
        }

        private void timerShowWin_Tick(object sender, EventArgs e)
        {
            ShowForm(bShow);
        }

        //关闭窗口
        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        //打开客户管理窗口
        private void tBtnClient_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (Form cForm in MdiChildren)
            {
                if (cForm.Name == "FormClientMana")
                {
                    cForm.Focus();
                    break;
                }
                i++;
            }
            if (i == MdiChildren.Count())
            {
                FormClientMana clientForm = new FormClientMana();
                clientForm.MdiParent = this;
                clientForm.Show();
            }
        }
        //打开汽车管理窗口
        private void tBtnCar_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (Form cForm in MdiChildren)
            {
                if (cForm.Name == "FormCarMana")
                {
                    cForm.Focus();
                    break;
                }
                i++;
            }
            if (i == MdiChildren.Count())
            {
                FormCarMana carForm = new FormCarMana();
                carForm.MdiParent = this;
                carForm.Show();
            }
        }
        //打开出租管理窗口
        private void tBtnLease_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (Form cForm in MdiChildren)
            {
                if (cForm.Name == "FormLeaseMana")
                {
                    cForm.Focus();
                    break;
                }
                i++;
            }
            if (i == MdiChildren.Count())
            {
                FormLeaseMana leaseForm = new FormLeaseMana();
                leaseForm.MdiParent = this;
                leaseForm.Show();
            }
        }
        //打开续租管理窗口
        private void tBtnRelet_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (Form cForm in MdiChildren)
            {
                if (cForm.Name == "FormXZMana")
                {
                    cForm.Focus();
                    break;
                }
                i++;
            }
            if (i == MdiChildren.Count())
            {
                FormXZMana xzForm = new FormXZMana();
                xzForm.MdiParent = this;
                xzForm.Show();
            }
        }
        //打开预租管理窗口
        private void tBtnYZ_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (Form cForm in MdiChildren)
            {
                if (cForm.Name == "FormYuZuMana")
                {
                    cForm.Focus();
                    break;
                }
                i++;
            }
            if (i == MdiChildren.Count())
            {
                FormYuZuMana yzForm = new FormYuZuMana();
                yzForm.MdiParent = this;
                yzForm.Show();
            }
        }
        //打开违章管理窗口
        private void tBtnPeccancy_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (Form cForm in MdiChildren)
            {
                if (cForm.Name == "FormWeiZhangMana")
                {
                    cForm.Focus();
                    break;
                }
                i++;
            }
            if (i == MdiChildren.Count())
            {
                FormWeiZhangMana wzForm = new FormWeiZhangMana();
                wzForm.MdiParent = this;
                wzForm.Show();
            }
        }

        private void tBtnSearch_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (Form cForm in MdiChildren)
            {
                if (cForm.Name == "FormSearch")
                {
                    cForm.Focus();
                    break;
                }
                i++;
            }
            if (i == MdiChildren.Count())
            {
                FormSearch cxForm = new FormSearch();
                cxForm.MdiParent = this;
                cxForm.Show();
            }
        }

        private void tpBtnBackCar_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (Form cForm in MdiChildren)
            {
                if (cForm.Name == "FormTHMana")
                {
                    cForm.Focus();
                    break;
                }
                i++;
            }
            if (i == MdiChildren.Count())
            {
                FormTHMana thForm = new FormTHMana();
                thForm.MdiParent = this;
                thForm.Show();
            }
        }
        //在状态栏上显示的内容
        int i = 0; string zmStr = "";
        private void timeShow_Tick(object sender, EventArgs e)
        {
            string sTime = "";
            sTime += DateTime.Now.Date.ToLongDateString() + " ";
            sTime += DateTime.Now.ToLongTimeString() + " ";
            switch (DateTime.Now.DayOfWeek)
            {
                case DayOfWeek.Friday: sTime += "星期五 "; break;
                case DayOfWeek.Monday: sTime += "星期二 "; break;
                case DayOfWeek.Saturday: sTime += "星期六 "; break;
                case DayOfWeek.Sunday: sTime += "星期日 "; break;
                case DayOfWeek.Thursday: sTime += "星期四 "; break;
                case DayOfWeek.Tuesday: sTime += "星期二 "; break;
                case DayOfWeek.Wednesday: sTime += "星期三 "; break;
            }
            labShowTime.Text = sTime;
            if (!string.IsNullOrEmpty(sCaption))
            {
                char[] arrStr = sCaption.ToCharArray();
                int iLength = arrStr.Length;
                if (i == iLength)
                {
                    i = 0; zmStr = "";
                }
                zmStr += arrStr[i];
                i++;
                labZiMu.Text = zmStr;
            }
        }

        //显示保存对话框
        private void tBtnBackup_Click(object sender, EventArgs e)
        {
            saveFile.Title = "请指定一个位置以保存数据备份文件";
            saveFile.ShowDialog();
        }

        //保存对话框的保存事件
        private void saveFile_FileOk(object sender, CancelEventArgs e)
        {
            //根据将被备份的数据库建立文件信息对象
            FileInfo fi = new FileInfo(Application.StartupPath + "\\dbCarMana.mdb");
            //在目标位置保存数据库,如果已存在则覆盖
            fi.CopyTo(saveFile.FileName, true);
            MessageBox.Show("恭喜您,数据库备份成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        //显示还原对话框
        private void tBtnRevert_Click(object sender, EventArgs e)
        {
            openFile.Title = "请选择一个已有的或备份的数据文件";
            openFile.ShowDialog();
        }

        private void openFile_FileOk(object sender, CancelEventArgs e)
        {
            //根据打开对话框指定文件建立文件信息对象
            FileInfo fi = new FileInfo(openFile.FileName);
            //将备份的文件添加并覆盖系统中的数据库
            fi.CopyTo(Application.StartupPath + "\\dbCarMana.mdb", true);
            MessageBox.Show("恭喜您,数据库还原成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void tBtnOption_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (Form cForm in MdiChildren)
            {
                if (cForm.Name == "FormSetting")
                {
                    cForm.Focus();
                    break;
                }
                i++;
            }
            if (i == MdiChildren.Count())
            {
                FormSetting seForm = new FormSetting();
                seForm.MdiParent = this;
                seForm.Show();
            }
        }

        private void tBtnAbout_Click(object sender, EventArgs e)
        {
            int i = 0;
            foreach (Form cForm in MdiChildren)
            {
                if (cForm.Name == "FormVersion")
                {
                    cForm.Focus();
                    break;
                }
                i++;
            }
            if (i == MdiChildren.Count())
            {
                FormVersion vForm = new FormVersion();
                vForm.MdiParent = this;
                vForm.Show();
            }
        }

        private void FormMain_MinimumSizeChanged(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
        }

        //当用户双击托盘区图标时,如果窗口处理最小化隐藏状态则显示出来
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (!this.ShowInTaskbar)
            {
                this.ShowInTaskbar = true;
                WindowState = FormWindowState.Normal;
            }
        }

        //当窗体状态改变时
        private void FormMain_Resize(object sender, EventArgs e)
        {
            //如果是最小化窗体,则隐藏其在任务栏上的显示
            if (WindowState == FormWindowState.Minimized)
                this.ShowInTaskbar = false;
        }

        private void 操作手册BToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("请按F1打开帮助!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
